package uutalk.netcloud.talk.base.util;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by hujie on 2017/12/11.
 */

public class FileSaveListener {

    /**
     * 保存的文件名 baseName + utteranceId， 通常是 output-0.pcm
     */
    private String baseName = "output-";

    /**
     * 保存文件的目录
     */
    private String destDir;

    /**
     * 文件
     */
    private File ttsFile;


    /**
     * ttsFile 文件流
     */
    private FileOutputStream ttsFileOutputStream;

    /**
     * ttsFile 文件buffer流
     */
    private BufferedOutputStream ttsFileBufferedOutputStream;


    /**
     * 创建以日期命名的mp3
     */
    public static String createFileName() {
        return System.currentTimeMillis() + ".pcm";
    }

    /**
     * 创建一个录音文件
     *
     * @param fileName
     * @return
     */
    private static File createRecorderFile(Context context, String fileName) {
        String recorderSavePath = getRootPath(context);
        File dir = new File(recorderSavePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(recorderSavePath, fileName);
        try {
            file.deleteOnExit();
            file.createNewFile();
        } catch (IOException e) {
            file = null;
        }
        return file;
    }

    private static String getRootPath(Context context) {
        if (context.getExternalCacheDir() != null) {
            return context.getExternalCacheDir().getAbsolutePath() + "/voice";
        } else if (isSdCardExist()) {
            return Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/voice";
        }
        return null;
    }

    private static boolean isSdCardExist() {
        return Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
    }


    public void onStartRecording(Context context, String fileId) {
        try {
            String filename = fileId + ".pcm";
            ttsFile = createRecorderFile(context, filename);
            // 创建FileOutputStream对象
            FileOutputStream ttsFileOutputStream = new FileOutputStream(ttsFile);
            // 创建BufferedOutputStream对象
            ttsFileBufferedOutputStream = new BufferedOutputStream(ttsFileOutputStream);

        } catch (IOException e) {
            // 请自行做错误处理
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    public void onRecording(byte[] bytes) {
        try {
            ttsFileBufferedOutputStream.write(bytes);
        } catch (IOException e) {
        }
    }

    public void onEndRecording() {
        close();
    }


    /**
     * 关闭流，注意可能stop导致该方法没有被调用
     */
    private void close() {
        if (ttsFileBufferedOutputStream != null) {
            try {
                ttsFileBufferedOutputStream.flush();
                ttsFileBufferedOutputStream.close();
                ttsFileBufferedOutputStream = null;
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        if (ttsFileOutputStream != null) {
            try {
                ttsFileOutputStream.close();
                ttsFileOutputStream = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取当前的录音文件
     * @return
     */
    public File getTtsFile() {
        return ttsFile;
    }


}
